<template>
    <div @click="startScan" style="font-size: 12px;display: flex;">
        <van-image :src="scanBlack" />
        <span style="font-size: 12px; margin-left: 3px;">
            {{ title }}
        </span>
    </div>
    
    <van-popup  v-model:show="showPop" round position="bottom" :style="{ height: '80%' }">
        <div>
            <p>扫描结果{{ scanResult }}</p>
            <iframe  @load="onIframeLoad($event)" :src="currentSrc"  frameborder="0" style="width: 100%; height: 500px;"></iframe>
        </div>
    </van-popup>
</template>
<script setup>
import uiUtil from "@/util/UiUtil.js";
import axios from 'axios'
import scanBlack from '@/assets/img/main/scan_black.png';

const scanResult = uiUtil.ref('')
const title = uiUtil.ref('')
const showPop = uiUtil.ref(false)
const currentSrc = uiUtil.ref('')
const iframeContent = uiUtil.ref('')

const emit = defineEmits(['scan-result']);

function startScan() {
    // 调用安卓端的方法启动扫码并直接返回结果
    if (window.Android && window.Android.scan) {
        window.Android.scan();
    } else {
        uiUtil.toastFail('扫码功能不可用'); 
    }
}

function onResult() {

    // showPop.value = true

    if (scanResult.value.startsWith('http://') || scanResult.value.startsWith('https://')) {
        // requestIframeContent(scanResult.value)
        currentSrc.value = scanResult.value
    } else {
        // uiUtil.toastFail('二维码格式不正确');
    } 
    
    emit('scan-result', scanResult.value);
}

window.handleScanResult = function (result) {
    scanResult.value = result 
    onResult()
    // uiUtil.toastFail('扫码结果: ' + result)
}


</script>

<style></style>